package com.helper.dream.a361phonehelper.adapter;


import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.helper.dream.a361phonehelper.R;
import com.helper.dream.a361phonehelper.bean.NewsBean;
import com.helper.dream.a361phonehelper.utils.ItemType;
import com.helper.dream.a361phonehelper.utils.L;
import com.helper.dream.a361phonehelper.widget.CircleImage;
import com.squareup.picasso.Picasso;

import java.util.List;


/**
 * Created by Administrator on 2016/12/19.
 */

public class DetailRecyclerAdapter extends RecyclerView.Adapter<DetailRecyclerAdapter.MyHolder>{

    Context context;
    List<NewsBean.DataBean.ResBean> list;
    LayoutInflater layoutInflater;

    public DetailRecyclerAdapter(Context context, List<NewsBean.DataBean.ResBean> list) {
        this.context = context;
        this.list = list;
        this.layoutInflater = LayoutInflater.from(context);
    }

    @Override
    public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = null;
        switch (viewType){
            case ItemType.A_VIDIO_TYPE:{
                view = layoutInflater.inflate(R.layout.item_a_video_layout, parent, false);
                return new MyHolder1(view);
            }
            default:{
                view = layoutInflater.inflate(R.layout.item_a_title_layout, parent, false);
                return new MyHolder(view);
            }
        }
    }

    @Override
    public void onBindViewHolder(MyHolder holder, int position) {
        NewsBean.DataBean.ResBean res = list.get(position);
        holder.tv_title.setText(res.getT());
        holder.tv_name.setText(res.getF());
        holder.tv_time.setText(list.size() + "");
        int type = getItemViewType(position);
        switch (type){
            case ItemType.A_VIDIO_TYPE:{
                MyHolder1 m = (MyHolder1) holder;
                Picasso.with(context).load(res.getFromicon())
                        .transform(new CircleImage())
                        .into(m.iv_header);
                Picasso.with(context).load(res.getI()).into(m.iv_video);
            }break;
            default:{
            }
        }
    }

    @Override
    public int getItemViewType(int position) {
        NewsBean.DataBean.ResBean res = list.get(position);
        L.d("res.getVideoUrl----------" + res.getVideoUrl());
        if(res.getVideoUrl() != null){
            return ItemType.A_VIDIO_TYPE;
        }
        return super.getItemViewType(position);
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    class MyHolder extends RecyclerView.ViewHolder{
        //一个标题
        TextView tv_title, tv_name, tv_time;
        public MyHolder(View itemView) {
            super(itemView);
            tv_title = (TextView) itemView.findViewById(R.id.tv_title);
            tv_name = (TextView) itemView.findViewById(R.id.tv_name);
            tv_time = (TextView) itemView.findViewById(R.id.tv_time);
        }
    }

    class MyHolder0 extends MyHolder{
        ImageView iv_one, iv_two, iv_three, iv_header;
        TextView tv_title, tv_comment;
        //三个图
        public MyHolder0(View itemView) {
            super(itemView);
            iv_one = (ImageView) itemView.findViewById(R.id.iv_one);
            iv_two = (ImageView) itemView.findViewById(R.id.iv_two);
            iv_three = (ImageView) itemView.findViewById(R.id.iv_three);
            iv_header = (ImageView) itemView.findViewById(R.id.iv_header);

            tv_title = (TextView) itemView.findViewById(R.id.tv_title);
            tv_comment = (TextView) itemView.findViewById(R.id.tv_comment);
        }
    }
    class MyHolder1 extends MyHolder{
        //有视频
        ImageView iv_video, iv_header;
        public MyHolder1(View itemView) {
            super(itemView);
            iv_video = (ImageView) itemView.findViewById(R.id.iv_video);
            iv_header = (ImageView) itemView.findViewById(R.id.iv_header);
        }
    }
    class MyHolder2 extends MyHolder{
        ImageView right_iv;

        //一个图
        public MyHolder2(View itemView) {
            super(itemView);
            right_iv = (ImageView) itemView.findViewById(R.id.right_iv);
        }
    }
    class MyHolder3 extends MyHolder{
        //一个标题
        ImageView iv_header;
        public MyHolder3(View itemView) {
            super(itemView);
            iv_header = (ImageView) itemView.findViewById(R.id.iv_header);
        }
    }
}
